package ch.jherzig.uebungen.blatt1;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Aufgabe2 {

	public static void main(String[] args) {

		String binary = null;
		int w_max;
		int v_max = 0;
		int w[];
		int v[];
		int n = 9;
		int pos;
		long startTime;

		w_max = 15;
		w = new int[n];
		v = new int[n];
		w[0] = 2;
		v[0] = 6;
		w[1] = 2;
		v[1] = 3;
		w[2] = 6;
		v[2] = 5;
		w[3] = 5;
		v[3] = 4;
		w[4] = 3;
		v[4] = 5;
		w[5] = 7;
		v[5] = 8;
		w[6] = 1;
		v[6] = 3;
		w[7] = 6;
		v[7] = 5;
		w[8] = 4;
		v[8] = 2;

		String[] mRes = new String[n];
		startTime = System.currentTimeMillis();
		
		pos = (int) Math.pow(2, n);
		
		for (int y = pos; y > 0; y--) {

			binary = Integer.toBinaryString(y);

			String[] m = new String[n];

			for (int i = 0; i < m.length; i++) {
				if (binary.length() > i) {
					m[i] = (String) binary.subSequence(i, i + 1);
				} else {
					m[i] = "0";
				}
			}

			int wi = 0;
			int vi = 0;

			for (int i = 0; i < n; i++) {
				if (m[i].endsWith("1")) {
					wi = wi + w[i];
					vi = vi + v[i];
				}
			}
			if (wi <= w_max) {
				if (vi > v_max) {
					v_max = vi;
					mRes = m;
				}
			}
		}
		binary = "";
		for (int i = 0; i < mRes.length; i++) {
			binary = binary + mRes[i];
		}

		System.out.println("Value: " + v_max);
		System.out.println("Binary: " + binary);
		System.out.println("Time needed: " + (System.currentTimeMillis() - startTime));

	}

}
